package com.brillilab.starter.controller.aio.experiments;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.brillilab.common.alioss.OssFileEnum;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.account.IntegralTypeEnum;
import com.brillilab.domain.enums.comments.CommentsClassifyEnum;
import com.brillilab.domain.enums.comments.CommentsContentTypeEnum;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.account.IntegralRecord;
import com.brillilab.domain.po.experiments.Experiment;
import com.brillilab.domain.po.experiments.ExperimentGroup;
import com.brillilab.domain.po.kit.Reagent;
import com.brillilab.domain.po.system.FileManage;
import com.brillilab.domain.vo.account.IntegralAddTipsVo;
import com.brillilab.domain.vo.comments.CommentsAddVo;
import com.brillilab.domain.vo.experiments.AnalyzeDataAddReturnVo;
import com.brillilab.domain.vo.experiments.AnalyzeDataVo;
import com.brillilab.domain.vo.experiments.EvaluationVo;
import com.brillilab.domain.vo.experiments.ExperimentAddVo;
import com.brillilab.domain.vo.experiments.ExperimentCommentAddVo;
import com.brillilab.domain.vo.experiments.ExperimentGroupRelationIdVo;
import com.brillilab.domain.vo.experiments.ExperimentGroupRelationTreeVo;
import com.brillilab.domain.vo.experiments.ExperimentGroupSaveVo;
import com.brillilab.domain.vo.experiments.ExperimentKitAddVo;
import com.brillilab.domain.vo.experiments.ExperimentProcessVo;
import com.brillilab.domain.vo.experiments.ExperimentProduceListGroupVo;
import com.brillilab.domain.vo.experiments.ExperimentProduceListGroupVoList;
import com.brillilab.domain.vo.experiments.ExperimentRelationListVo;
import com.brillilab.domain.vo.experiments.ExperimentRelationVo;
import com.brillilab.domain.vo.experiments.ExperimentTipsVo;
import com.brillilab.domain.vo.experiments.ExperimentTroubleShootingListVo;
import com.brillilab.domain.vo.experiments.ExperimentTroubleShootingVo;
import com.brillilab.domain.vo.experiments.PrimaryDataAddReturnVo;
import com.brillilab.domain.vo.experiments.PrimaryDataVo;
import com.brillilab.domain.vo.kit.ReagentDetailsVo;
import com.brillilab.domain.vo.kit.SampleInfoVo;
import com.brillilab.domain.vo.method.MethodDetail;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.experiments.IExperimentGroupService;
import com.brillilab.service.core.experiments.IExperimentProduceService;
import com.brillilab.service.core.experiments.IExperimentService;
import com.brillilab.service.core.experiments.IPlateService;
import com.brillilab.service.core.kit.IReagentService;
import com.brillilab.service.core.system.IFileManageService;
import com.brillilab.service.core.topics.ITopicExperimentService;
import com.brillilab.service.logic.account.UserAccountLogic;
import com.brillilab.service.logic.comments.CommentsLogic;
import com.brillilab.service.logic.experiments.ExperimentLogic;
import com.brillilab.service.logic.kit.ReagentLogic;
import com.brillilab.service.logic.kit.SampleLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import com.brillilab.starter.entity.req.experiments.MethodChangeReq;

/**
 * 新实验
 *
 * @time 2019/09/05
 */
@RestController
@RequestMapping("/api/experiment")
public class ExperimentController {

	@Resource
	private ExperimentLogic experimentLogic;
	@Resource
	private UsersLogic usersLogic;
	@Resource
	private ReagentLogic reagentLogic;
	@Resource
	private IReagentService reagentService;
	@Resource
	private SampleLogic sampleLogic;
	@Resource
	private UserAccountLogic userAccountLogic;
	@Resource
	private IExperimentProduceService experimentProduceService;
	@Resource
	private IExperimentService experimentService;
	@Resource
	private ITopicExperimentService topicExperimentService;
	@Resource
	private IExperimentGroupService experimentGroupService;
	@Resource
	private IPlateService plateService;
	@Resource
	private CommentsLogic commentsLogic;
	@Resource
	private IFileManageService fileManageService;

	/**
	 * 无方法添加实验
	 *
	 * @return
	 */
	@AspectLog
	@PostMapping("/addExperiment")
	public ResponseVo addExperiment(@RequestBody ExperimentAddVo addVo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(addVo != null && addVo.getExperimentId() != null && StringUtils.isNotBlank(addVo.getName())
				&& StringUtils.isNotBlank(addVo.getContent()), "请求参数缺失!");
		Map<String, Long> map = experimentLogic.addExperiment(addVo);
		return ResponseVo.success(map);
	}

	/**
	 * 是否是课题目标下的第一次成功符合预期实验结果 是否默认设为发表级结果
	 *
	 * @param topicExperimentTargetId
	 * @param token
	 * @return
	 */
	@RequestMapping(value = "/isFirstResult", method = RequestMethod.GET)
	public ResponseVo isFirstResult(@RequestParam Long topicExperimentTargetId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(topicExperimentTargetId != null && StringUtils.isNotBlank(token), "请求参数缺失！");
		return ResponseVo.success(topicExperimentService.hasPublishResult(topicExperimentTargetId));
	}

	/**
	 * 添加tips-直传
	 *
	 * @param experimentTipsVo
	 * @param token
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/addTips", method = RequestMethod.POST)
	public ResponseVo addTips(@RequestBody ExperimentTipsVo experimentTipsVo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(experimentTipsVo != null && StringUtils.isNotBlank(token), "请求参数缺失！");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		Experiment experiment = experimentService.selectById(experimentTipsVo.getExperimentId());
		Assert.isTrue(experiment != null && experiment.getLabId().equals(userInfo.getLabId()), "实验不属于该实验室！");
		Assert.isTrue(experiment.getExecutorId().equals(userInfo.getLabMemberId()), "不是实验的执行人！");
		// 文件名检测
		if (CollectionUtils.isNotEmpty(experimentTipsVo.getTipsFileList())) {
			for (FileManage file : experimentTipsVo.getTipsFileList()) {
				Assert.isTrue(!file.getName().contains("%"), "文件名包含特殊字符，请核实！");
				break;
			}
		}
		List<FileManage> tipsFileList = experimentLogic.addTips(experimentTipsVo, userInfo);
		return ResponseVo.success(tipsFileList);
	}

	/**
	 * 添加tips-续传
	 *
	 * @param experimentTipsVo
	 * @param token
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/addTips/renewal", method = RequestMethod.POST)
	public ResponseVo addTipsRenewal(@RequestBody ExperimentTipsVo experimentTipsVo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(experimentTipsVo != null && StringUtils.isNotBlank(token), "请求参数缺失！");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		Experiment experiment = experimentService.selectById(experimentTipsVo.getExperimentId());
		Assert.isTrue(experiment != null && experiment.getLabId().equals(userInfo.getLabId()), "实验不属于该实验室！");
		Assert.isTrue(experiment.getExecutorId().equals(userInfo.getLabMemberId()), "不是实验的执行人！");
		experimentTipsVo.setRenewal(true);
		// 文件名检测
		if (CollectionUtils.isNotEmpty(experimentTipsVo.getTipsFileList())) {
			for (FileManage file : experimentTipsVo.getTipsFileList()) {
				Assert.isTrue(!file.getName().contains("%"), "文件名包含特殊字符，请核实！");
				break;
			}
		}
		List<FileManage> tipsFileList = experimentLogic.addTips(experimentTipsVo, userInfo);
		return ResponseVo.success(tipsFileList);
	}

	/**
	 * 获得tips
	 *
	 * @param stepId
	 * @param token
	 * @return
	 */
	@RequestMapping(value = "/getTips", method = RequestMethod.GET)
	public ResponseVo getTips(@RequestParam Long stepId, @RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(stepId != null && StringUtils.isNotBlank(token), "请求参数缺失！");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		Experiment experiment = experimentService.selectExperimentByStepId(stepId);
		Assert.isTrue(userInfo.getLabId().equals(experiment.getLabId()), "实验不属于该实验室！");
		return ResponseVo.success(experimentLogic.getTips(stepId));
	}

	/**
	 * 删除tips文本
	 *
	 * @param stepId
	 * @param token
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/deleteTips", method = RequestMethod.GET)
	public ResponseVo deleteTips(@RequestParam Long stepId, @RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(stepId != null && StringUtils.isNotBlank(token), "请求参数缺失！");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		Experiment experiment = experimentService.selectExperimentByStepId(stepId);
		Assert.isTrue(experiment.getLabId().equals(userInfo.getLabId())
				&& experiment.getExecutorId().equals(userInfo.getLabMemberId()), "没有操作权限！");
		experimentLogic.deleteTips(stepId);
		return ResponseVo.success();
	}

	/**
	 * 实验tips列表
	 *
	 * @param experimentId
	 * @param token
	 * @return
	 */
	@RequestMapping(value = "/tipsList", method = RequestMethod.GET)
	public ResponseVo tipsList(@RequestParam Long experimentId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(experimentId != null && StringUtils.isNotBlank(token), "请求参数缺失！");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		Experiment experiment = experimentService.selectById(experimentId);
		Assert.isTrue(experiment.getLabId().equals(userInfo.getLabId()), "没有操作权限！");
		return ResponseVo.success(experimentLogic.tipsList(experimentId));
	}

	/**
	 * 实验评论数量
	 *
	 * @param experimentId
	 * @param token
	 * @return
	 */
	@Deprecated
	@RequestMapping(value = "/commentNum", method = RequestMethod.GET)
	public ResponseVo commentNum(@RequestParam Long experimentId, Integer type,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		return ResponseVo.successMap("commentNum", experimentLogic.commentNum(experimentId, type));
	}

	/**
	 * 实验分组树型结构
	 *
	 * @param experimentId
	 * @return
	 */
	@GetMapping("/group/tree")
	public ResponseVo getExperimentGroupTree(Long experimentId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(experimentId != null, "请求参数缺失！");
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		List<ExperimentGroupRelationTreeVo> groupList = experimentLogic.getExperimentGroupTree(experimentId,
				userInfoVo);
		boolean isLeader = experimentLogic.isLeader(experimentId, userInfoVo);
		if (isLeader) {
			experimentService.experimentGuideSetting(experimentId, 1, 2);
		}
		return ResponseVo.success(groupList);
	}

	/**
	 * 实验分组列表
	 *
	 * @param experimentId
	 * @return
	 */
	@GetMapping("/group/list")
	public ResponseVo getExperimentGroupList(Long experimentId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(experimentId != null, "请求参数缺失！");
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		List<ExperimentGroup> groupList = experimentLogic.getExperimentGroupList(experimentId, userInfoVo);
		boolean isLeader = experimentLogic.isLeader(experimentId, userInfoVo);
		if (isLeader) {
			experimentService.experimentGuideSetting(experimentId, 1, 2);
		}
		return ResponseVo.success(groupList);
	}

	/**
	 * 实验分组详情
	 *
	 * @param experimentId
	 * @return
	 */
	@GetMapping("/group/detail")
	public ResponseVo getExperimentGroupDetail(Long experimentId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(experimentId != null, "请求参数缺失！");
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		Map<String, List<ExperimentGroup>> groupMap = experimentLogic.getExperimentGroupDetail(experimentId,
				userInfoVo);
		return ResponseVo.success(groupMap);
	}

	/**
	 * 实验分组添加
	 *
	 * @param save
	 * @return
	 */
	@PostMapping("/group/save")
	public ResponseVo saveExperimentGroup(@RequestBody ExperimentGroupSaveVo save,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(save != null && save.getExperimentId() != null, "请求参数缺失！");
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		experimentLogic.saveExperimentGroup(save, userInfoVo.getLabId());
		experimentService.experimentGuideSetting(save.getExperimentId(), 1,
				CollectionUtils.isNotEmpty(save.getGroups()) ? 1 : 0);

		if (CollectionUtils.isNotEmpty(save.getGroups())) {
			commentsLogic.sendExperimentCard(userInfoVo.getLabId(), userInfoVo.getLabMemberId(), save.getExperimentId(),
					userInfoVo.getName()+"编辑了实验分组");
		}
		return ResponseVo.success();
	}

	/**
	 * 实验分组已有分组列表
	 *
	 * @param experimentId
	 * @return
	 */
	@GetMapping("/allGroup")
	public ResponseVo getAllGroup(Long experimentId, @RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(experimentId != null, "请求参数缺失！");
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		return ResponseVo.success(experimentLogic.getAllGroup(experimentId, userInfoVo.getLabId()));
	}

	/**
	 * @Description: 删除单个实验分组
	 * @Param [vo]
	 */
	@PostMapping("/group/del")
	public ResponseVo deleteExperimentGroup(@RequestBody ExperimentGroupRelationIdVo vo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(vo != null && vo.getId() != null && vo.getExperimentId() != null, "请求参数缺失！");
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		experimentLogic.deleteExperimentGroup(vo, userInfoVo.getLabId());
		List<ExperimentGroup> groupList = experimentGroupService.selectList(vo.getExperimentId());
		if (CollectionUtils.isEmpty(groupList)) {
			experimentService.experimentGuideSetting(vo.getExperimentId(), 1, 0);
		}
		
		return ResponseVo.success();

	}

	/**
	 * 实验评论新增-续传
	 *
	 * @param add
	 * @param token
	 * @return
	 */
	@Deprecated
	@PostMapping("/comment/addRenewal")
	public ResponseVo addRenewalComment(@RequestBody ExperimentCommentAddVo add,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(add != null && add.getExperimentId() != null, "请求参数缺失！");
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		add.setRenewal(true);
		// 文件名检测
		if (CollectionUtils.isNotEmpty(add.getCommentFileList())) {
			for (FileManage file : add.getCommentFileList()) {
				Assert.isTrue(!file.getName().contains("%"), "文件名包含特殊字符，请核实！");
				break;
			}
		}
		experimentLogic.addComment(add, userInfoVo);
		return ResponseVo.success();
	}

	/**
	 * 实验评论新增-直传
	 *
	 * @param add
	 * @param token
	 * @return
	 */
	@Deprecated
	@PostMapping("/comment/add")
	public ResponseVo addComment(@RequestBody ExperimentCommentAddVo add,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(add != null && add.getExperimentId() != null, "请求参数缺失！");
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		add.setRenewal(false);
		experimentLogic.addComment(add, userInfoVo);
		
		CommentsAddVo addVo = new CommentsAddVo();
		addVo.setClassify(CommentsClassifyEnum.EXPERIMENT.getValue());
		addVo.setClassifyId(add.getExperimentId());
		addVo.setContent(add.getContent());
		addVo.setType(add.getType()!=null?add.getType():0);
		addVo.setContentType(CommentsContentTypeEnum.TEXT.getValue());
		if (!ArrayUtils.isEmpty(add.getCommentFileIds())) {
			Long fileId = add.getCommentFileIds()[0];
			
			FileManage fileManage = fileManageService.selectById(fileId);
			fileManage.setId(null);
			fileManage.setReferDataId(0L);
			fileManage.setAttachType(OssFileEnum.CommentFile.getKey());
			FileManage file = fileManageService.insert(fileManage);
			addVo.setFileId(file.getId());
			if(fileManage.getFileType().equals(0)) {
				addVo.setContentType(CommentsContentTypeEnum.FILE.getValue());
			}else if(fileManage.getFileType().equals(1)) {
				addVo.setContentType(CommentsContentTypeEnum.VOICE.getValue());
			}else if(fileManage.getFileType().equals(2)) {
				addVo.setContentType(CommentsContentTypeEnum.VIDEO.getValue());
			}else if(fileManage.getFileType().equals(3)) {
				addVo.setContentType(CommentsContentTypeEnum.PICTURE.getValue());
			}
		}
		commentsLogic.addComments(userInfoVo.getLabId(), userInfoVo.getLabMemberId(), addVo);
		
		return ResponseVo.success();
	}

	/**
	 * 实验评论列表
	 *
	 * @param experimentId
	 * @param token
	 * @return
	 */
	@Deprecated
	@GetMapping("/commentList")
	public ResponseVo getCommentList(Long experimentId, Integer type,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(experimentId != null, "请求参数缺失！");
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		return ResponseVo.success(experimentLogic.commentList(experimentId, type, userInfoVo));
	}

	/**
	 * 实验评论删除
	 *
	 * @param id
	 * @return
	 */
	@Deprecated
	@DeleteMapping("/comment")
	public ResponseVo deleteComment(Long id, @RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(id != null, "请求参数缺失！");
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		experimentLogic.deleteComment(id, userInfoVo);
		return ResponseVo.success();
	}

	/**
	 * 新增troubleShooting-直传附件关联
	 *
	 * @param listVo
	 * @param token
	 * @return
	 */
	@PostMapping("/troubleShooting")
	public ResponseVo addTroubleShooting(@RequestBody ExperimentTroubleShootingVo listVo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(listVo.getExperimentId() != null, "参数缺失！");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		experimentLogic.addTroubleShooting(listVo, userInfo);
		experimentService.experimentGuideSetting(listVo.getExperimentId(), 3,
				CollectionUtils.isNotEmpty(listVo.getTroubleShootingList()) ? 1 : 0);
		IntegralRecord integralRecord = userAccountLogic.addIntegral(userInfo, IntegralTypeEnum.EXPERIMENT,
				listVo.getExperimentId());
		IntegralAddTipsVo tipsVo = new IntegralAddTipsVo();
		if (integralRecord != null) {
			tipsVo.setTntegralTips("恭喜您，完成上传数据");
		}
		return ResponseVo.success(tipsVo);
	}

	/**
	 * 获取troubleShooting
	 *
	 * @param experimentId
	 * @return
	 */
	@GetMapping("/troubleShootingList")
	public ResponseVo getTroubleShooting(Long experimentId, @RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(experimentId != null, "参数缺失！");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		List<ExperimentTroubleShootingListVo> troubleShooting = experimentLogic.getTroubleShooting(experimentId);

		boolean isLeader = experimentLogic.isLeader(experimentId, userInfo);
		if (isLeader) {
			experimentService.experimentGuideSetting(experimentId, 3, 2);
		}
		return ResponseVo.success(troubleShooting);
	}

	/**
	 * 获取上一次实验troubleShooting
	 *
	 * @param experimentId
	 * @return
	 */
	@GetMapping("/lastTroubleShooting")
	public ResponseVo getLastTroubleShooting(Long experimentId) {
		Assert.isTrue(experimentId != null, "参数缺失！");
		List<ExperimentTroubleShootingListVo> lastTroubleShooting = experimentLogic
				.getLastTroubleShooting(experimentId);
		return ResponseVo.success(lastTroubleShooting);
	}

	/**
	 * troubleShooting方法/试剂/分组
	 *
	 * @param experimentId
	 * @return
	 */
	@GetMapping("/troubleShootingMatters")
	public ResponseVo troubleShootingMatters(Long experimentId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(experimentId != null, "参数缺失！");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		return ResponseVo.success(experimentLogic.troubleShootingMatters(experimentId, userInfo));
	}

	/**
	 * 新增分析后数据
	 *
	 * @param dataVo
	 * @param token
	 * @return
	 */
	@PostMapping("/analyzeData")
	public ResponseVo addAnalyzeData(@RequestBody AnalyzeDataVo dataVo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(dataVo.getExperimentId() != null, "参数缺失");
		Assert.isTrue(dataVo.getOriginFileId() != null || dataVo.getOriginFile() != null, "附件缺失！");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		// 文件名检测
		if (dataVo.getOriginFile() != null) {
			Assert.isTrue(!dataVo.getOriginFile().getName().contains("%"), "分析后数据原附件-文件名包含特殊字符，请核实！");
		}
		// 文件名检测
		if (dataVo.getCompressFile() != null) {
			Assert.isTrue(!dataVo.getCompressFile().getName().contains("%"), "分析后数据缩略附件-文件名包含特殊字符，请核实！");
		}
		AnalyzeDataAddReturnVo returnVo = experimentLogic.addAnalyzeData(dataVo, userInfo);
		experimentService.experimentGuideSetting(dataVo.getExperimentId(), 2, 1);
		commentsLogic.sendExperimentCard(userInfo.getLabId(), userInfo.getLabMemberId(), dataVo.getExperimentId(),
				userInfo.getName()+"上传了分析后数据");
		
		IntegralRecord integralRecord = userAccountLogic.addIntegral(userInfo, IntegralTypeEnum.EXPERIMENT,
				dataVo.getExperimentId());
		IntegralAddTipsVo tipsVo = new IntegralAddTipsVo();
		if (integralRecord != null) {
			tipsVo.setTntegralTips("恭喜您，完成上传数据");
		}
		returnVo.setTipsVo(tipsVo);
		return ResponseVo.success(returnVo);
	}

	/**
	 * 分析后数据列表
	 *
	 * @param experimentId
	 * @return
	 */
	@GetMapping("/analyzeData")
	public ResponseVo analyzeData(Long experimentId, @RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(experimentId != null, "参数缺失！");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		FileManage analyzeData = experimentLogic.getAnalyzeData(experimentId);
		if (analyzeData != null) {
			boolean isLeader = experimentLogic.isLeader(experimentId, userInfo);
			if (isLeader) {
				experimentService.experimentGuideSetting(experimentId, 2, 2);
			}
		}
		return ResponseVo.success(analyzeData);
	}

	/**
	 * 删除分析后数据
	 *
	 * @param experimentId
	 * @return
	 */
	@DeleteMapping("/analyzeData")
	public ResponseVo deleteAnalyzeData(Long experimentId) {
		Assert.isTrue(experimentId != null, "参数缺失！");
		experimentLogic.deleteAnalyzeDate(experimentId);
		experimentService.experimentGuideSetting(experimentId, 2, 0);
		return ResponseVo.success();
	}

	/**
	 * 实验评价设置
	 *
	 * @param evaluationVo
	 * @return
	 */
	@PostMapping("/evaluation")
	public ResponseVo evaluationSetting(@RequestBody EvaluationVo evaluationVo) {
		Assert.isTrue(evaluationVo.getExperimentId() != null && evaluationVo.getResult() != null
				&& evaluationVo.getPublish() != null, "参数缺失！");
		experimentLogic.evaluationSetting(evaluationVo);
		return ResponseVo.success();
	}

	/**
	 * 实验试剂列表
	 *
	 * @param experimentId
	 * @return
	 */
	@GetMapping("/reagentList")
	public ResponseVo getReagentList(Long experimentId, Long phaseId, Long stepId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(experimentId != null, "参数缺失！");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		return ResponseVo.success(experimentLogic.getExperimentReagentList(experimentId, phaseId, stepId, userInfo));
	}

	/**
	 * 原始数据新增
	 *
	 * @param primaryDataVo
	 * @param token
	 * @return
	 */
	@PostMapping("/primaryData")
	public ResponseVo addPrimaryData(@RequestBody PrimaryDataVo primaryDataVo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(primaryDataVo.getExperimentId() != null, "参数缺失！");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		// 文件名检测
		if (CollectionUtils.isNotEmpty(primaryDataVo.getFileList())) {
			for (FileManage file : primaryDataVo.getFileList()) {
				Assert.isTrue(!file.getName().contains("%"), "文件名包含特殊字符，请核实！");
				break;
			}
		}
		PrimaryDataAddReturnVo returnVo = experimentLogic.primaryData(primaryDataVo, userInfo);
		return ResponseVo.success(returnVo);
	}

	/**
	 * 原始数据列表
	 *
	 * @param experimentId
	 * @return
	 */
	@GetMapping("/primaryDataList")
	public ResponseVo primaryDateList(Long experimentId, @RequestHeader(value = StarterConstant.TOKEN) String token) {
		Assert.isTrue(experimentId != null, "参数缺失！");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		return ResponseVo.success(experimentLogic.primaryDateList(experimentId, userInfo));
	}

	/**
	 * 方法切换
	 *
	 * @param req
	 * @param token
	 * @return
	 * @author WuMenghap
	 */
	@PutMapping("/method")
	public ResponseVo changeExperimentMethod(@RequestBody MethodChangeReq req,
			@RequestHeader(value = StarterConstant.TOKEN) String token) {
		Assert.isTrue(req != null && req.getExperimentId() != null && req.getNewMethodId() != null, "请求参数缺失!");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		experimentLogic.changeExperimentMethod(req.getExperimentId(), req.getNewMethodId() ,req.getGlobalKitId(), userInfo);
		return ResponseVo.success();
	}

	/**
	 * 方法详情
	 *
	 * @param experimentId
	 * @param token
	 * @return
	 * @author WuMenghao
	 */
	@GetMapping("/methodDetail")
	public ResponseVo getExperimentMethodDetail(Long experimentId,
			@RequestHeader(value = StarterConstant.TOKEN) String token) {
		Assert.notNull(experimentId, "请求参数缺失!");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		MethodDetail detail = experimentLogic.getExperimentMethodDetail(experimentId, userInfo.getLabId());
		return ResponseVo.success(detail);
	}

	/**
	 * 实验产出列表
	 *
	 * @param experimentId
	 * @param token
	 * @return
	 * @author WuMenghao
	 */
	@GetMapping("/productList")
	public ResponseVo getProductionList(Long experimentId, @RequestHeader(value = StarterConstant.TOKEN) String token) {
		Assert.notNull(experimentId, "请求参数缺失!");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		List<ExperimentProduceListGroupVo> produceGroups = experimentLogic.getExperimentProductions(experimentId,
				userInfo.getLabId());
		return ResponseVo.success(produceGroups);
	}

	/**
	 * 实验产出列表
	 *
	 * @param experimentId
	 * @param token
	 * @return
	 * @author WuMenghao
	 */
	@GetMapping("/relateProductList")
	public ResponseVo getRelateProductionList(Long experimentId,
			@RequestHeader(value = StarterConstant.TOKEN) String token) {
		Assert.notNull(experimentId, "请求参数缺失!");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		List<ExperimentProduceListGroupVoList> produceGroupsList = experimentLogic
				.getRelateExperimentProductions(experimentId, userInfo.getLabId());
		return ResponseVo.success(produceGroupsList);
	}

	/**
	 * 实验 添加试剂
	 *
	 * @param kitAdd
	 * @param token
	 * @return
	 * @author WuMenghao
	 */
	@PostMapping("/reagent")
	public ResponseVo addReagentToExperiment(@RequestBody ExperimentKitAddVo kitAdd,
			@RequestHeader(value = StarterConstant.TOKEN) String token) {
		Assert.isTrue(
				kitAdd != null && kitAdd.getExperimentId() != null && CollectionUtils.isNotEmpty(kitAdd.getItems()),
				"请求参数缺失!");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		experimentLogic.addReagentToExperiment(kitAdd, userInfo);
		return ResponseVo.success();
	}

	/**
	 * 试剂列表 移出试剂
	 *
	 * @param experimentId
	 * @param token
	 * @return
	 * @author WuMenghao
	 */
	@DeleteMapping("/reagent")
	public ResponseVo removeExperimentReagent(Long sourceId, Integer source, Long experimentId,
			@RequestHeader(value = StarterConstant.TOKEN) String token) {
		Assert.isTrue(sourceId != null && source != null && experimentId != null, "请求参数缺失!");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		experimentLogic.removeExperimentReagent(sourceId, source, experimentId, userInfo.getLabId(),
				userInfo.getLabMemberId());
		return ResponseVo.success();
	}

	/**
	 * 实验关联样品列表
	 *
	 * @param experimentId
	 * @param token
	 * @return
	 * @author LJ
	 */
	@GetMapping("/relationList")
	public ResponseVo relationExperimentList(Long experimentId,
			@RequestHeader(value = StarterConstant.TOKEN) String token) {
		Assert.notNull(experimentId, "请求参数缺失!");
		List<ExperimentRelationListVo> listVo = experimentLogic.getRelationList(experimentId);
		return ResponseVo.success(listVo);
	}

	/**
	 * 实验关联样品
	 *
	 * @param token
	 * @return
	 * @author LJ
	 */
	@PostMapping("/relation")
	public ResponseVo experimentRelation(@RequestBody ExperimentRelationVo relationVo,
			@RequestHeader(value = StarterConstant.TOKEN) String token) {
		Assert.notNull(relationVo.getExperimentId(), "请求参数缺失!");
		experimentLogic.experimentRelation(relationVo);
		return ResponseVo.success();
	}

	/**
	 * 实验试剂详情
	 *
	 * @param sourceId
	 * @param source   来源：1-产出,2-成分
	 * @param token
	 * @return
	 */
	@GetMapping("/reagentDetails")
	public ResponseVo reagentDetails(@RequestParam Long sourceId, @RequestParam Integer source,
			@RequestHeader(value = StarterConstant.TOKEN) String token) {
		Assert.isTrue(source == 2, "请求参数错误!");
		Reagent reagent = reagentService.selectById(sourceId);
		ReagentDetailsVo details = reagentLogic.getReagentDetails(reagent.getKitId());
		return ResponseVo.success(details);
	}

	/**
	 * 实验样品详情
	 *
	 * @param sourceId
	 * @param source   来源：1-产出,2-成分
	 * @param token
	 * @return
	 */
	@GetMapping("/sampleDetails")
	public ResponseVo sampleDetails(@RequestParam Long sourceId, @RequestParam Integer source,
			@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo urs = usersLogic.getUserByToken(token);
		SampleInfoVo details = null;
		if (source == 2) {
			Reagent reagent = reagentService.selectById(sourceId);
			details = sampleLogic.getSampleInfo(reagent.getKitId(), urs.getLabMemberId());
		} else if (source == 1) {
			details = experimentProduceService.getProduceDetails(sourceId, urs.getLabMemberId());
		}
		return ResponseVo.success(details);

	}

	/**
	 * 是否有分组或多孔板
	 *
	 * @param experimentId
	 * @param token
	 * @return
	 */
	@GetMapping("/isHaveGroupOrPlate")
	public ResponseVo isHaveGroupOrPlate(@RequestParam Long experimentId,
			@RequestHeader(value = StarterConstant.TOKEN) String token) {
		int isHaveGroupOrPlate = BoolEnum.FALSE.getValue();
		boolean isHaveGroup = experimentGroupService.isExistByExperimentId(experimentId);
		if (isHaveGroup) {
			isHaveGroupOrPlate = BoolEnum.TRUE.getValue();
		} else {
			int plateNum = plateService.getPlateCount(experimentId);
			isHaveGroupOrPlate = plateNum > 0 ? BoolEnum.TRUE.getValue() : BoolEnum.FALSE.getValue();
		}
		return ResponseVo.successMap("isHaveGroupOrPlate", isHaveGroupOrPlate);

	}

	/**
	 * 实验进度，返回所有步骤状态
	 * 
	 * @param experimentId
	 * @param token
	 * @return
	 */
	@RequestMapping(value = "/process", method = RequestMethod.GET)
	public ResponseVo experimentProcess(Long experimentId, @RequestHeader(value = StarterConstant.TOKEN) String token) {
		Assert.notNull(experimentId, "请求参数缺失!");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		ExperimentProcessVo process = experimentLogic.getExperimentProcess(experimentId, userInfo);
		return ResponseVo.success(process);
	}
}
